﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

public partial class View_Admin_QuanLySanPham : System.Web.UI.Page
{
    QLHoa hoa = new QLHoa();
    QLTheLoai tl = new QLTheLoai();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            GridView1.DataSource = hoa.Load_QLHoa();
            GridView1.DataBind();
            dropLoaiHoa.DataSource = tl.Load_TheLoai();
            dropLoaiHoa.DataValueField = "maloai";
            dropLoaiHoa.DataTextField = "tenloai";
            dropLoaiHoa.DataBind();
        }
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        string hinhanh=Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/hinhsp/")+hinhanh);
        //float gia = float.Parse(txtGia.Text);
        //hoa.Insert_Hoa(txtTenHoa.Text, txtNguonGoc.Text, txtYNghia.Text, gia, "~/Image/Hoa/" + hinhanh);
        string mahoa = hoa.Insert_Hoa(txtTenHoa.Text, txtNguonGoc.Text, txtYNghia.Text, txtGia.Text, "~/hinhsp/" + hinhanh);
        hoa.Insert_CTLoaiHoa(dropLoaiHoa.SelectedValue, mahoa, int.Parse(txtSl.Text));
        GridView1.DataSource = hoa.Load_QLHoa();
        GridView1.DataBind();
        lblThongBao.Text = "Đã thêm thành công !!!";
        txtGia.Text = "";
        txtNguonGoc.Text = "";
        txtYNghia.Text = "";
        txtTenHoa.Text = "";
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = hoa.Load_QLHoa();
        GridView1.DataBind();

        DropDownList drop_loaihoa = (DropDownList)GridView1.Rows[e.NewEditIndex].Cells[5].FindControl("DropDownList1");
        drop_loaihoa.DataSource = tl.Load_TheLoai();
        drop_loaihoa.DataValueField = "maloai";
        drop_loaihoa.DataTextField = "tenloai";
        drop_loaihoa.DataBind();
        drop_loaihoa.Items.FindByValue(GridView1.DataKeys[e.NewEditIndex].Values[1].ToString()).Selected = true;

        GridView1.Rows[e.NewEditIndex].Cells[0].Enabled = false;
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridView1.DataSource = hoa.Load_QLHoa();
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
     
        string mahoa = GridView1.DataKeys[e.RowIndex].Values[3].ToString();
        TextBox tenhoa = (TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0];
        TextBox nguongoc = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
        TextBox ynghia = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
        TextBox giahoa = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
        DropDownList Maloai = (DropDownList)GridView1.Rows[e.RowIndex].Cells[4].FindControl("DropDownList1");
        TextBox sl = (TextBox)GridView1.Rows[e.RowIndex].Cells[5].FindControl("TextBox1");

        FileUpload hinhanh = (FileUpload)GridView1.Rows[e.RowIndex].Cells[5].FindControl("FileUpload2");
        string duongdan = Path.GetFileName(hinhanh.PostedFile.FileName);
        if (hinhanh.HasFile)
        {
            hinhanh.PostedFile.SaveAs(Server.MapPath("~/hinhsp/") + duongdan);
             hoa.Update_Hoa(mahoa, tenhoa.Text, nguongoc.Text, ynghia.Text, giahoa.Text, "~/hinhsp/" + duongdan);
            string sl_Old = GridView1.DataKeys[e.RowIndex].Values[2].ToString();
            if (sl == null)
                hoa.Update_CTLoaiHoa(Maloai.SelectedValue, mahoa, sl_Old);
            else
                hoa.Update_CTLoaiHoa(Maloai.SelectedValue, mahoa, sl.Text);
        }
        else
        {
            string dd = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
            hoa.Update_Hoa(mahoa, tenhoa.Text, nguongoc.Text, ynghia.Text, giahoa.Text, dd);
            string sl_Old = GridView1.DataKeys[e.RowIndex].Values[2].ToString();
            if (sl == null)
                hoa.Update_CTLoaiHoa(Maloai.SelectedValue, mahoa, sl_Old);
            else
                hoa.Update_CTLoaiHoa(Maloai.SelectedValue, mahoa, sl.Text);
        }
        GridView1.EditIndex = -1;
        GridView1.DataSource = hoa.Load_QLHoa();
        GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string mahoa = (string)GridView1.Rows[e.RowIndex].Cells[0].Text.ToString();
        GridView1.DataSource = hoa.Delete_CTLoaiHoa(mahoa);
        GridView1.DataSource= hoa.Delete_Hoa(mahoa);
        GridView1.DataSource = hoa.Load_QLHoa();
        GridView1.DataBind();
    }
}
